package com.cqc.jdk8.Jlang.thread;

import org.junit.Test;

import java.util.concurrent.*;

/**
 * @author cui
 * @date 2025/6/15 17:35
 * @description 创建线程的几种方法
 **/
public class ThreadTest {

    @Test
    public void f1() {
        AThread thread = new AThread();
//        thread.start();//Thread-0....AThread.run()
        thread.run();//main....AThread.run()  run方法不会创建线程
    }

    @Test
    public void f2() {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName() + "....Runnable.run()");
            }
        });
        thread.start();
    }


    @Test
    public void f3() throws Exception {
        Callable<String> callable = new Callable<String>() {
            @Override
            public String call() throws Exception {
                System.out.println(Thread.currentThread().getName() + "....callable.call()");
                return "A";
            }
        };
        FutureTask<String> futureTask = new FutureTask<String>(callable);
        Thread thread = new Thread(futureTask);
        thread.start();
        System.out.println(futureTask.get(2, TimeUnit.SECONDS));
    }

    @Test
    public void f4() {
        ExecutorService executorService = Executors.newFixedThreadPool(2);
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                //pool-1-thread-1....ExecutorService.call()  项目只有1个线程池时
                System.out.println(Thread.currentThread().getName() + "....ExecutorService.call()");
            }
        });
        executorService.shutdown();
    }


    public class AThread extends Thread {
        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName() + "....AThread.run()");
        }
    }
}
